package com.wiken.storeservice.controller;


import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.VO.UserStoreActivityVO;
import com.wiken.storeservice.entity.UserStoreActivity;
import com.wiken.storeservice.service.impl.UserStoreActivityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2023-02-08
 */
@RestController
@RequestMapping("/storeservice/userstoreactivity")
public class UserStoreActivityController {
    @Autowired
    UserStoreActivityServiceImpl userStoreActivityService;

    @PostMapping("addUserStoreActivity")
    public R addUserStoreActivity(@RequestBody UserStoreActivity userStoreActivity, @RequestHeader("Authorization") String token){
        boolean success = userStoreActivityService.addUserStoreActivity(userStoreActivity, token);

        if(!success) throw new DefaultException(Code.ERROR, "获取优惠劵失败");

        return R.ok().code(200).message("正在尝试抢夺优惠劵");
    }

    @GetMapping("getUserStoreActivity")
    public R getUserStoreActivity(@RequestHeader("Authorization") String token){
        ArrayList<UserStoreActivityVO> userStoreActivity = userStoreActivityService.getUserStoreActivity(token);

        return R.ok().code(200).message("获取优惠劵成功").data("userStoreActivityList", userStoreActivity);
    }

    @GetMapping("getUserStoreActivityByStoreId")
    public R getUserStoreActivityByStoreId(@RequestParam("storeId") String storeId, @RequestHeader("Authorization") String token){
        ArrayList<UserStoreActivityVO> userStoreActivity = userStoreActivityService.getUserStoreActivityByStoreId(storeId, token);

        return R.ok().code(200).message("获取优惠劵成功").data("userStoreActivityList", userStoreActivity);
    }

    @PostMapping("useStoreActivity")
    public boolean useStoreActivity(@RequestBody String userStoreActivityId, @RequestHeader("Authorization") String token){
        boolean success = userStoreActivityService.useUserStoreActivity(userStoreActivityId, token);

        return success;
    }
}
